﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Net;
using System.IO;

namespace RegionData
{
    public class RegionData
    {
        private const int MAX_HTTP_ATTEMPTS = 3;
        private string requestUrl = null;
        private int serverId = 0;
        public bool Init(string _requestUrl, string _ip, int _port)
        {
            requestUrl = _requestUrl + "/api/updatestate.php";
            bool result = false;
            string url = string.Concat(requestUrl, "?type=region&cmd=addserver&i=", _ip, "&p=", _port);
            string response = WebRequest(url);
            string[] paras = response.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            if (paras.Length == 2 && string.Compare(paras[0], "ok") == 0 && int.TryParse(paras[1], out serverId) && serverId > 0) 
            {
                result = true;
            }
            return result;
        }
        public void UpdateState()
        {
            string url = string.Concat(requestUrl, "?type=region&cmd=update&s=", serverId);
            WebRequest(url);
        }
        public bool AddPlayer(int backgroundId, int regionId, int playerId, int count)
        {
            bool result = false;
            string url = string.Concat(requestUrl, "?type=player&cmd=add&r=", regionId, "&b=", backgroundId, "&c=", count + 1, "&p=", playerId, "&s=", serverId);
            string response = WebRequest(url);
            string[] paras = response.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int cmd = 0;
            if (paras.Length == 2 && string.Compare(paras[0], "ok") == 0 && int.TryParse(paras[1], out cmd) && cmd == 1)
            {
                result = true;
            }
            return result;
        }
        public bool DeletePlayer(int backgroundId, int regionId, int playerId, int count)
        {
            bool result = false;
            string url = string.Concat(requestUrl, "?type=player&cmd=delete&r=", regionId, "&b=", backgroundId, "&c=", count - 1, "&p=", playerId, "&s=", serverId);
            string response = WebRequest(url);
            if (string.Compare(response, "ok") == 0)
            {
                result = true;
            }
            return result;
        }
        public int AddDomain(int backgroundId, string name)
        {
            int regionId = 0;
            string url = string.Concat(requestUrl, "?type=region&cmd=addregion&b=", backgroundId, "&n=", name, "&s=", serverId);
            string response = WebRequest(url);
            string[] paras = response.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            if (paras.Length == 2 && string.Compare(paras[0], "ok") == 0 && int.TryParse(paras[1], out regionId) && regionId > 0) { }
            return regionId;
        }
        public bool DeleteDomain(int regionId)
        {
            bool result = false;
            string url = string.Concat(requestUrl, "?type=region&cmd=deleteregion&r=", regionId);
            string response = WebRequest(url);
            if (string.Compare(response, "ok") == 0)
            {
                result = true;
            }
            return result;
        }
        public void Close()
        {
            string url = string.Concat(requestUrl, "?type=region&cmd=deleteserver&s=", serverId);
            WebRequest(url);
        }
        private string WebRequest(string url)
        {
            string response = String.Empty;
            Log.Debug("WebRequest(): " + url);
            for (int i = 0; i < MAX_HTTP_ATTEMPTS; ++i)
            {
                try
                {
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                    request.Timeout = 60000;
                    WebResponse webresponse = request.GetResponse();
                    using (StreamReader reader = new StreamReader(webresponse.GetResponseStream()))
                    {
                        response = reader.ReadToEnd();
                    }
                    break;
                }
                catch (WebException e)
                {
                    response = String.Empty;
                    Log.Error("Web Request to " + url + " failed. Error:" + e.Message);
                }
            }
            return response.Trim();
        }
    }
}
